<?php
session_start();
if(isset($_SESSION['userNum']) && $_SESSION['voter_permission'] == 'Voter'){
	$userNum = $_SESSION['userNum']; //set userNum 
	$pageTitle = $_SESSION['title'];
	$curPos = $_SESSION['candPos']; // set the current position - preesident,vice, etc.
	$server = $_SESSION['server'];
	$user = $_SESSION['user'];
	$pass = $_SESSION['pass'];
	$db = $_SESSION['db'];

	$ifLoginIsSingle = checkSessionID($_SESSION['sid'],$userNum,$server,$user,$pass,$db);
		
		if($ifLoginIsSingle == "False"){
			session_destroy(); 
			header('Location: home.php');
		}
	
}else{
header('Location: home.php');
}
?>
<html>
<head>
<title>Results - <?php echo $pageTitle ?></title>
<?php include('showIcon.php'); ?>
<?php include('loadingScript.html'); ?>
</head>
<body style="background-image:url('images/bg_blue.jpg')">
<div style="position:absolute;left:75px;top: 0px">
<font face = "Arial">
<table style="width: 810px; height: 104px" border="0" cellspacing="0" cellpadding="0" >
<?php include("menuButtons.php"); ?>
</table>
<table style="width: 810px;" cellspacing="0" cellpadding="0" >
<tr>
<td style="height: 76px; width: 28px;"></td>
<td style="height: 76px" width="808px">

<?php

$hasResults = "<font color='red'>None</font";
$hasNoResults = "<font color='red'>None</font>";
$posNoResults = "";

echo "<br><h3>Positions with election results</h3>";
mysql_connect($server,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$positions = mysql_query("Select * from positions");
while($row = mysql_fetch_array($positions))
{
	if($row['pos_close_vote']==1){
		$hasResults = "";
		$limit = $row['pos_vote_limit'];
		$ctr = 0;	
		$arrInfo=array();
		$arrCount=array();		
		
		mysql_connect($server,$user,$pass) or die(mysql_error());
		mysql_select_db($db) or die(mysql_error());
		$candidates = mysql_query("Select * from candidates where Cand_Pos = '".$row['pos_num']."'");
		
		$tempPosName = $row['pos_name'];
		$tempPosNum = $row['pos_num'];
		$tempPosLimit = $row['pos_vote_limit'];
		
			while($candInfo = mysql_fetch_array($candidates))
			{
					$tempCandName=$candInfo['Cand_Name'];
					$tempCandParty=$candInfo['Cand_Party'];
					$tempCandNum = $candInfo['Cand_Num'];
					$arrInfo[$ctr] = $tempCandNum."|".$tempCandName."|".$tempCandParty;
			
					mysql_connect($server,$user,$pass) or die(mysql_error());
					mysql_select_db($db) or die(mysql_error());
					$voteResults = mysql_query("SELECT c.cand_name as 'pangalan', c.cand_party as 'Candidate',count(*) as 'Votes' from candidates c,vote_count v where v.voted_cand_num = '".$tempCandNum."' and c.cand_pos = '".$tempPosNum."' group by cand_num order by Votes desc");
			
					$count = 0;	
					
					while($voteCounts = mysql_fetch_array($voteResults)){
					$count = $voteCounts['Votes'];
					}
					
					$arrCount[$ctr] = $count;
					$ctr = $ctr+1;
					
			}
			
		$ctr2 = 0;
		echo "<table border = '0' width = '600px'><tr><td colspan='3' width='320'><h4><font color=#0066CC>".$tempPosName."<font> - Election Results</td></tr>";
		echo "<tr style = 'background-color:silver'><td width = '200px'>Name</td><td  width = '100px'>Party</td><td  width = '50px'>Votes</td></tr>";
		$sorted = insertionSort($arrCount,$arrInfo,count($arrInfo));
		while($ctr2 < count($sorted)){
			$pieces = explode("|",$sorted[$ctr2]);
			if($ctr2 < $tempPosLimit){
			echo "<tr bgcolor=#494541 ><td width='200'><a href='candidates.php?pos=".$tempPosNum."&name=".$tempPosName."#".$pieces[0]."' style='text-decoration:none;font-size:17'><font color=#CCFFCC>".$pieces[1]." (Winner)</font></a></td><td width='100'><font color='white'>".$pieces[2]."</font></td><td align = 'center' width='50'><font color='white'>".$pieces[3]."</font></td></tr>";
			}else{
			echo "<tr bgcolor=#CCFFFF><td width='200'><a href='candidates.php?pos=".$tempPosNum."&name=".$tempPosName."#".$pieces[0]."' style='text-decoration:none;font-size:17'><font color='gray'>".$pieces[1]."</font></a></td><td width='100'>".$pieces[2]."</td><td align = 'center' width='50'>".$pieces[3]."</td></tr>";
			}
			
			$ctr2 = $ctr2 + 1;
		}
		echo "</table><br>";
				
		
	}else{
		$hasNoResults = "";
		$posNoResults = $posNoResults.$row['pos_name']."<br>";
	}	
}
	echo $hasResults;
	
	echo "<hr>";
	echo "<h3>Positions with election ongoing</h3>";
	echo $hasNoResults;
	echo $posNoResults;

	if(strlen($hasNoResults) == 0){
	echo "<br><a href='vote.php' style='font-size:12'><b>Vote now!</b></a>";
	}
	
function insertionSort($list,$names, $length) {

	
	/* The bubble sort method.  If you don't know how it works it's very
	 * simple, values are switched one at a time for each element. */
	for($x = 0; $x < $length; $x++) {
	  for($y = 0; $y < $length; $y++) {
		if($list[$x] > $list[$y]) {
		  $hold = $list[$x];
		  $hold2 = $names[$x];
		  $list[$x] = $list[$y];
		  $names[$x] = $names[$y];
		  $list[$y] = $hold;
		  $names[$y] = $hold2;
		}
	  }
	}
	
	$ctr = 0;
	while($ctr < count($list)){
	$newArray[$ctr] = $names[$ctr]."|".$list[$ctr];
	$ctr+=1;
	}
	return $newArray;
}

//Function checkSessionID()
// -Checks if session in db is same in the session in browser
// If not, it logs out
function checkSessionID($sid,$uname,$sver,$usr,$passwd,$dbase){
mysql_connect($sver,$usr,$passwd) or die(mysql_error());
mysql_select_db($dbase) or die(mysql_error());

	$querySession = mysql_query("SELECT voter_current_session from voters WHERE voter_num = '".$uname."'");	

		while($sessionInDB = mysql_fetch_array($querySession)){
		$loggedSession = $sessionInDB['voter_current_session'];
		}

	if($_SESSION['sid'] == $loggedSession){
	return "True";
	}else{
	return "False";
	}	
}
?>
</td>
</tr>
</table>
<br><br><br><br><br>
</div>
<?php include('bottomLinks.php'); ?>
</body>
</html>
